package common

import (
	"context"
	"git.mycaigou.com/gfyx/common/http_client"
	"git.mycaigou.com/gfyx/common/simplecopier"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	common2 "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/common"
	reportDto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/oppty/report"
	"strings"
)

type CommonRepository struct {
}

func NewCommonRepository() CommonReposIface {
	return &CommonRepository{}
}

// GetLocation 获取地址列表
func (r *CommonRepository) GetLocation(ctx context.Context) ([]map[string]interface{}, error) {
	params := map[string]interface{}{
		"pageNum":  1,
		"pageSize": 999,
	}
	resultData := http_client.BigDataResponse{}

	_, _, errs := http_client.NewBigdataApiHttpClient().Get(ctx, constants.GetLocation, params).EndStruct(&resultData)
	if errs != nil {
		return nil, errs[0]
	}
	return resultData.Data.Rows, nil
}

func (r *CommonRepository) FuzzySearchCompanyInfo(ctx context.Context, keyword string) (interface{}, error) {
	params := map[string]interface{}{
		"keyword": keyword,
	}
	resultData := http_client.OpenApiResponse{}
	_, _, errs := http_client.NewOpenApiHttpClient().Post(ctx, constants.FuzzySearch, params, map[string]string{}).EndStruct(&resultData)
	if errs != nil {
		return nil, errs[0]
	}
	return resultData.Data, nil
}

func (r *CommonRepository) FillReportForm(ctx context.Context, params *reportDto.FillReportFormRequest) (*http_client.BigDataResponseList, error) {
	resultData := &http_client.BigDataResponseList{}
	header := map[string]string{}

	bigdataParamsMap, err := simplecopier.StructToMapByJsonTag(params)
	if err != nil {
		return resultData, err
	}

	var errs []error
	if strings.EqualFold(params.ClueSource, "developer") || strings.EqualFold(params.ClueSource, "supplier") {
		_, _, errs = http_client.NewBigdataApiHttpClient().Post(ctx, constants.GetFillDeveloperReportUrl, bigdataParamsMap, header).EndStruct(&resultData)
	} else if params.ClueSource == "PURCHASE" {
		_, _, errs = http_client.NewBigdataApiHttpClient().Post(ctx, constants.GetFillPurchaseReportUrl, bigdataParamsMap, header).EndStruct(&resultData)
	} else if params.ClueSource == "WINBID" {
		_, _, errs = http_client.NewBigdataApiHttpClient().Post(ctx, constants.GetFillWinbidReportUrl, bigdataParamsMap, header).EndStruct(&resultData)
	} else if params.ClueSource == "LAND" {
		_, _, errs = http_client.NewBigdataApiHttpClient().Post(ctx, constants.GetFillLandReportUrl, bigdataParamsMap, header).EndStruct(&resultData)
	} else if params.ClueSource == "BUILDING" {
		_, _, errs = http_client.NewBigdataApiHttpClient().Post(ctx, constants.GetFillBuildingReportUrl, bigdataParamsMap, header).EndStruct(&resultData)
	} else if params.ClueSource == "PROJECT" {
		_, _, errs = http_client.NewBigdataApiHttpClient().Post(ctx, constants.GetFillProjectReportUrl, bigdataParamsMap, header).EndStruct(&resultData)
	} else if strings.EqualFold(params.ClueSource, "sale") {
		_, _, errs = http_client.NewBigdataApiHttpClient().Post(ctx, constants.GetFillSaleReportUrl, bigdataParamsMap, header).EndStruct(&resultData)
	}

	if errs != nil {
		return nil, errs[0]
	}

	return resultData, nil
}

func (r *CommonRepository) GetProjectListByDeveloperName(ctx context.Context, req *common2.GetProjectListByCompanyNameRequest) ([]map[string]interface{}, int, error) {
	bigdataParamsMap, _ := simplecopier.StructToMapByJsonTag(req)
	delete(bigdataParamsMap, "project_name")
	delete(bigdataParamsMap, "company_type")
	if req.ProjectName != "" {
		bigdataParamsMap["project_title_like"] = "%" + req.ProjectName + "%"
	}
	resultData := http_client.BigDataResponse{}
	_, _, errs := http_client.NewBigdataApiHttpClient().Post(ctx, constants.GetDeveloperProjectList, bigdataParamsMap, map[string]string{}).EndStruct(&resultData)
	if errs != nil {
		return nil, 0, errs[0]
	}
	return resultData.Data.Rows, resultData.Data.TotalNum, nil
}

func (r *CommonRepository) GetProjectListBySupplierName(ctx context.Context, req *common2.GetProjectListByCompanyNameRequest) ([]map[string]interface{}, int, error) {
	bigdataParamsMap, _ := simplecopier.StructToMapByJsonTag(req)
	bigdataParamsMap["supplier_name"] = req.CompanyName
	delete(bigdataParamsMap, "project_name")
	delete(bigdataParamsMap, "company_type")
	delete(bigdataParamsMap, "company_name")
	if req.ProjectName != "" {
		bigdataParamsMap["coop_prj_name_like"] = "%" + req.ProjectName + "%"
	}
	resultData := http_client.BigDataResponse{}
	_, _, errs := http_client.NewBigdataApiHttpClient().Post(ctx, constants.GetSupplierProjectList, bigdataParamsMap, map[string]string{}).EndStruct(&resultData)
	if errs != nil {
		return nil, 0, errs[0]
	}
	return resultData.Data.Rows, resultData.Data.TotalNum, nil
}
